package com.wakey.rtcs.web.controller;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wakey.rtcs.miniui.PageManager;
import com.wakey.rtcs.web.model.File;
import com.wakey.rtcs.web.service.FileService;

@Controller
@RequestMapping("/file")
public class DocFileController {
	@Autowired
	private FileService fileService;

	@RequestMapping(value = "saveDocFile")
	@ResponseBody
	public boolean saveDocFile(File file){
		int num = fileService.saveFile(file);	
		return num>0 ? true:false;
	}
	
	@RequestMapping(value = "/queryAllFile")
	@ResponseBody
	public PageManager<File> queryAllFile(HttpServletRequest request, PageManager<File> page,File file) {
		List<File> list = fileService.queryAllFile(file);
		page.setData(list);
		return page;
	}
	@RequestMapping(value = "/queryFileList")
	@ResponseBody
	public PageManager<File> queryFileList(HttpServletRequest request, PageManager<File> page,Long docId,Integer userType, Long userId) {
		List<File> list = fileService.queryFileList(docId,userType,userId);
		page.setData(list);
		return page;
	}
	@RequestMapping(value = "/queryLastestFile")
	@ResponseBody
	public PageManager<File> queryLastestFile(HttpServletRequest request, PageManager<File> page,Long docId,Integer userType, Long userId) {
		List<File> list = fileService.queryLastestFile(docId,userType,userId);
		page.setData(list);
		return page;
	}
	@RequestMapping(value = "/deleteFile")
	@ResponseBody
	public boolean deleteFile(Long id) {
		int num = fileService.deleteFile(id);
		return num>0 ? true:false;
	}
	@RequestMapping("download")  
	public void download(HttpServletRequest request,  HttpServletResponse response, String filePath, String fileName) throws IOException {
		String suffix = "."+filePath.substring(filePath.lastIndexOf(".") + 1);
		fileName += suffix;
		String dfileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); 
	    HttpHeaders headers = new HttpHeaders(); 
	    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);       
	    headers.setContentDispositionFormData("attachment", dfileName);
        java.io.File file = new java.io.File(request.getRealPath(filePath));
        
        // 设置相应头，控制浏览器下载该文件，这里就是会出现当你点击下载后，出现的下载地址框
        response.setHeader("content-disposition","attachment;filename=" + dfileName);
     
        // 读取要下载的文件，保存到文件输入流
        FileInputStream in = new FileInputStream(file);
        // 创建输出流
        OutputStream out = response.getOutputStream();
        // 创建缓冲区
        byte buffer[] = new byte[1024];
        int len = 0;
        // 循环将输入流中的内容读取到缓冲区中
        while ((len = in.read(buffer)) > 0) {
            // 输出缓冲区内容到浏览器，实现文件下载
            out.write(buffer, 0, len);
        }
        // 关闭文件流
        in.close();
        // 关闭输出流
        out.close();
	    //return new ResponseEntity<byte[]>(body, headers, HttpStatus.OK); 

	}

	@RequestMapping(value = "checkFile")
	@ResponseBody
	public boolean checkFile(Long id,Integer check,String advice,Long checkMan){
		int num = fileService.checkFile(id,check,advice,checkMan);	
		return num>0 ? true:false;
	}
	
	
}
